import { isNull } from "lodash-es";
import type { TreeDataItem } from "ant-design-vue/es/tree";

/**
 * 格式化TreeTable
 * @param data
 * @returns {Array}
 */
export function formatTreeTable(data: TreeNode[]) {
  const treeData: any = [];

  data.forEach((node: TreeNode) => {
    const item: any = node.item;
    if (node.children != undefined && !isNull(node.children)) {
      item.children = formatTreeTable(node.children);
    }
    treeData.push(item);
  });
  return treeData;
}

/**
 * 格式化TreeSelect
 * @param data
 */
export function formatTreeSelect(data: TreeNode[]) {
  const treeData: TreeDataItem[] = [];

  data.forEach((node: TreeNode) => {
    const item: TreeDataItem = {
      key: node.id,
      title: node.name,
      value: node.id
    };
    if (node.children != undefined && !isNull(node.children)) {
      item.children = formatTreeSelect(node.children);
    }
    treeData.push(item);
  });
  return treeData;
}
